<?php

/*
 +-------------------------------------------------------------------+
 |                   browser.ctl.php                                 |
 |                                                                   |
 | Copyright ?                          www.gde.sclab.clarkson.edu   |
 | Author: Demetrios Dimatos            www.dimatos.net              |
 | Created: August 2006                 Last modified: Oct 15, 2006  |
 +-------------------------------------------------------------------+
 | This is the control file for browser.php that contains all the    |
 | logic.                                                            |
 |  - /gde-1.0/templates/logged-in/browser.php                       |
 +-------------------------------------------------------------------+
*/

//Start the SESSION vars
// if (session_id() == "")
// {
//	 session_start();
// }
 // Include configuration variables
 include_once('/var/www/gde-1.0/htdocs/config.php');
 // Include browse functions
 include_once(Config::base_path.Config::browse_func_path."browse.func.php");
 // Include html functions
 include_once(Config::base_path.Config::html_func_path."html.php");
 // Include database connnection
 include_once(Config::base_path.Config::stats_func_path."statistics.func.php");
 // Include the Graph class for generating some histogram graphs
 include_once(Config::base_path.Config::graph_func_path."graphs.inc.php");




 // Checks if variables are set for browsing schema if so, set them.
 if(isset($_GET['database'])) 
 {
  	$database = $_GET['database'];
  	
  	if(isset($_GET['table'])) 
  	{
    	$table = $_GET['table'];
    	if(isset($_GET['field'])) 
    	{
      	$field = $_GET['field'];
    	}
  	}
 }
 
 // Session variable set for DOMAIN_SPECIFIC queries
 if(isset($databaseName))
 {
 		$_SESSION['databaseName'] = $databaseName;
 }
 
 if(!isset($database))	//databases is not set
 {
  	$result = getDatabaseDescription(BACKEND);
  	message2html("List of available Databases",black,center);
  	databaseDescription2url($result);
 }
 else // $database is set
 {
 	  // Convert databaseID to Database Name
 		$databaseName = getDatabaseName($database);
 
 		if(!isset($table))
 		{
 			// Get the Database's table names using DatabaseID, note I had to use 2 calls
 			// for some reason ADODB does not just let me copy the tables to another var.
 			$tables 			= getTableNames($database);
 			$tables_copy 	= getTableNames($database);
 			
 			// Get table(s) Descriptions using tables
 			$table_Descriptions = getTablesDescription($tables,1);
 			
 			// Print the talbe(s) with there name and table descriptions
 			message2html("The tables for $databaseName",blac,center);
 			tableDescription2url($tables_copy,$database,$table_Descriptions);
  		
 		}
 		else	// $table is set
 		{
 			if(!isset($field))
 			{
 				// Get the table description of as single table after user has selected a table.
 				$table_description = getTableDescription($table,1);
 				
 				// Print the table name and the description in a table
 				message2html("Desciption for $table", black,center);	
 				$array_temp = array($table_description->fields[0]);
 				nRecord2Html($array_temp,"","","","","","","","","","","",1);
 				//record2Html($table_description, $table.' Description');  // second option to above
 				
 				// Get the number of records in the table
 				$num_records = recordCount($databaseName,$table,DOMAIN_SPECIFIC);
 			  
 				// Get the average record size in bytes.
 				$record_size = recordAvgSize($databaseName,$table,DOMAIN_SPECIFIC,$num_records);
 				
 				// Get the total database size
 				$table_size = recordSumSize($record_size,$num_records);
 				
 				// Set up array with stats in it
 				$array_stats = array("Number of records","Record size (bytes)","Size in database (bytes)");
 				$array_values = array($num_records,$record_size,$table_size);
 				message2html("Summary of statitics for $table", black, center);	
 				nRecord2Html($array_stats,$array_values,"","","","","","","","","","",2);
 				
 				// Get Database Description and print to screen
 				$described_database = describeDatabase($databaseName,$table);
 				message2html("The fields for $table", black,center);
 				fieldDescription2url($described_database,$database,$table);
 			
 				// Get 10 rows and print it to the screen
 			  $few_rows = selectLimit('select * from '.$databaseName.'.'.$table,10,DOMAIN_SPECIFIC);
 				message2html("Displaying a few rows from $table", black, center);
 				record2Html($few_rows);
 				
 				// Generate a histogram of the selected table
 				$data = doQuery('Select * from '.$databaseName.'.'.$table,DOMAIN_SPECIFIC,ADODB_FETCH_NUM);
 				message2html("Displaying the histogram for $table", black, center);
 				// Conver the result to and array
 			  $histogram_data = result2array($data);
 				// Generate the histogram and get only the array values for graph usage
 				$histogram_values = array_values(array_count_values($histogram_data));
 				// Get only the keys from the histram for graph usage
 				$histogram_keys = array_keys(array_count_values($histogram_data));
 				// Call the graph function
 				showGraph($histogram_keys,$histogram_values, "Freq: ","Item's percentage");
 				
 			}
 			else	// $field is set
 			{
 				// Display values for a field
 				$query = "SELECT DISTINCT $field FROM $databaseName.$table";
 				message2html("Displaying values for $table from $field",black,center);
 				$field_values = selectLimit($query,20,DOMAIN_SPECIFIC);
 				record2Html($field_values);
 			}
 		}
 }
?>